home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C++ / Frameworks / TransSkel++ 305 / Source / FakeAlert.cp next >
Text File  |  1996-01-17  |  7KB  |  243 lines

  1. /*
  2.  * FakeAlert.cp -- A C++ version for TransSkel++.  The main
  3.  * difference is that FakeAlert is now a private member function
  4.  * of the CApplObj class so that TransSkel++ programmers can still
  5.  * use FakeAlert.c.  Also rubbed inclusion of TransEdit.h.
  6.  * (fadushin)
  7.  *
  8.  * In-memory item list for dialog with five items:
  9.  *
  10.  * 1    Button 1
  11.  * 2    Button 2
  12.  * 3    Button 3
  13.  * 4    caution icon
  14.  * 5    "^0^1^2^3" (static text)
  15.  * 6    user item (for outlining default button)
  16.  *
  17.  * The caller of FakeAlert passes the four strings that are to be
  18.  * substituted into the first item, the number of buttons that
  19.  * should be used, and the titles to put into each button.
  20.  * A copy of the item list is hacked to use the right number of
  21.  * buttons.
  22.  *
  23.  * Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  24.  * this is modified from code they wrote.
  25.  *
  26.  * 17 Jun 92
  27.  * - Set the aDefItem field of the dialog template, so that return/enter
  28.  * key properly equivalence to clicking the default button.
  29.  * 05 Jun 93
  30.  * - Conversion for THINK C 6.0.
  31.  * 05 Jul 93
  32.  * - Repositioned buttons.  All are now in single line.  Button 1 is
  33.  * rightmost, button 2 in middle, and button 3 is leftmost.  As more
  34.  * buttons are used, they extend more to the left.
  35.  * 18 Dec 93
  36.  * - Took out all the "register" declarations.  The compiler's smart enough
  37.  * now that they don't make any difference, so they're just clutter.
  38.  * - Item list handle made unpurgable while ModalDialog() is processing
  39.  * dialog and purgable afterward.
  40.  * 18 Jan 94
  41.  * - Redid item list to provide a user item.  Default button outlining
  42.  * done using SkelSetDlogButtonOutliner().
  43.  * - Unused buttons are now hidden.  No more messing with changing the number
  44.  * of items in the item list.
  45.  * - Buttons positioned more in line with Apple guidelines.
  46.  * - Alert window positioned on the front window's parent device now.
  47.  * - FakeAlert() takes a parameter indicating the Cancel button.  This allows
  48.  * it to do key mapping for Escape and Command-period.
  49.  * 20 Jan 94
  50.  * - Added a caution icon to the alert item list.
  51.  */
  52.  
  53. # include    <TransSkel++.h>  // changed to TransSkel++ (fadushin)
  54.  
  55. // removed inclusion of TransEdit.h (fadushin)
  56.  
  57. /*
  58.  * Doesn't appear to be necessary
  59.  * # include    "TransEdit.h"
  60.  */
  61.  
  62.  
  63. /* in-memory item list */
  64.  
  65.  
  66. typedef enum
  67. {
  68.     button1Item = 1,
  69.     button2Item,
  70.     button3Item,
  71.     cautionIconItem,
  72.     staticTextItem,
  73.     outlineItem
  74. };
  75.  
  76.  
  77. static short    itemList [] =
  78. {
  79.     5,                    /* max number of items - 1 */
  80.  
  81.     /*
  82.      * first button
  83.      */
  84.  
  85.     0, 0,                                /* reserve a long for item handle */
  86.     105, 260, 125, 340,                    /* display rectangle */
  87.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  88.  
  89.     /*
  90.      * second button
  91.      */
  92.  
  93.     0, 0,                                /* reserve a long for item handle */
  94.     105, 167, 125, 247,                    /* display rectangle */
  95.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  96.  
  97.     /*
  98.      * third button
  99.      */
  100.  
  101.     0, 0,                                /* reserve a long for item handle */
  102.     105, 20, 125, 100,                    /* display rectangle */
  103.     ((ctrlItem+btnCtrl) << 8) | 0,        /* pushButton, title 0 bytes long */
  104.  
  105.     /*
  106.      * icon item
  107.      */
  108.  
  109.     0, 0,                                /* reserve a long for item handle */
  110.     10, 10, 42, 42,                        /* display rectangle */
  111.     ((iconItem+itemDisable) << 8) | 2,    /* icon (disabled), icon id 2 bytes long */
  112.     2,                /* icon 2 */
  113.  
  114.     /*
  115.      * statText item
  116.      */
  117.  
  118.     0, 0,                                /* reserve a long for item handle */
  119.     10, 52, 58, 340,                    /* display rectangle */
  120.     ((statText+itemDisable) << 8) | 8,    /* statText (disabled), text 8 bytes long */
  121.     '^0', '^1',        /* ^0^1^2^3 */
  122.     '^2', '^3',
  123.  
  124.     /*
  125.      * user item for default button outlining
  126.      */
  127.  
  128.     0, 0,                                /* reserve a long for item handle */
  129.     0, 0, 10, 10,                            /* display rectangle (fake) */
  130.     ((userItem+itemDisable) << 8) | 0,    /* userItem (disabled), title 0 bytes long */
  131. };
  132.  
  133.  
  134. /*
  135.  * Hide a dialog button item by shifting it off into space.  HideDItem()
  136.  * does the same thing but unfortunately is not present on 64K ROM
  137.  * machines. Inside Macintosh IV-59 says you can hide an item by adding
  138.  * 16384 to the left and right coordinates of the bounding rectangle.
  139.  * However, that doesn't seem to work unless the control itself is moved
  140.  * as well.
  141.  */
  142.  
  143. static void
  144. HideDlogButton (DialogPtr dlog, short item)
  145. {
  146. Rect    r;
  147.  
  148.     SkelGetDlogRect (dlog, item, &r);
  149.     OffsetRect (&r, 16384, 0);
  150.     SkelSetDlogRect (dlog, item, &r);
  151.     MoveControl (SkelGetDlogCtl (dlog, item), 16384, 0);
  152. }
  153.  
  154.  
  155. /*
  156.  * Fake an alert, using an in-memory window and item list.
  157.  * The message to be presented is constructed from the first
  158.  * four arguments.  nButtons is the number of buttons to use,
  159.  * defButton is the default button, the next three args are
  160.  * the titles to put into the buttons.  The return value is
  161.  * the button number (1..nButtons).  This must be interpreted
  162.  * by the caller, since the buttons may be given arbitrary
  163.  * titles.
  164.  *
  165.  * nButtons should be between 1 and 3, inclusive.
  166.  * defButton should be between 1 and nButtons, inclusive.
  167.  * cancelButton should be between 1 and nButtons, inclusive, or
  168.  * 0 if there's no cancel button.
  169.  */
  170.  
  171. // make FakeAlert a member function of the CApplObj class (fadushin)
  172.  
  173. short
  174. CApplObj::FakeAlert (StringPtr s1, StringPtr s2, StringPtr s3, StringPtr s4,
  175.             short nButtons, short defButton, short cancelButton,
  176.             StringPtr t1, StringPtr t2, StringPtr t3)
  177. {
  178. ModalFilterUPP    filter;
  179. GrafPtr        savePort;
  180. DialogPtr    dlog;
  181. Handle        iListHandle;
  182. Rect        bounds;
  183. short        item;
  184.  
  185.     InitCursor ();
  186.     GetPort (&savePort);
  187.     iListHandle = NewHandle (512L);
  188.     HLock (iListHandle);
  189.     BlockMove (&itemList, *iListHandle, 512L);
  190.     HUnlock (iListHandle);
  191.     HNoPurge (iListHandle);
  192.     SetRect (&bounds, 0, 0, 350, 135);
  193.     dlog = NewDialog (nil, &bounds, "\p", false, dBoxProc, (WindowPtr) -1L,
  194.                             false, 0L, iListHandle);
  195.     SkelPositionWindow (dlog, skelPositionOnParentDevice,
  196.                             FixRatio (1, 2), FixRatio (1, 5));
  197.     ((DialogPeek) dlog)->aDefItem = defButton;
  198.     SkelSetDlogButtonOutliner (dlog, outlineItem);
  199.  
  200.     SetPort (dlog);
  201.  
  202.     if (nButtons < 3)                /* hide unused buttons */
  203.     {
  204.         HideDlogButton (dlog, button3Item);
  205.         if (nButtons < 2)
  206.             HideDlogButton (dlog, button2Item);
  207.     }
  208.  
  209.     switch (nButtons)                /* set button titles */
  210.     {
  211.     case 3:
  212.         SetCTitle (SkelGetDlogCtl (dlog, button3Item), t3);
  213.         /* fall through... */
  214.     case 2:
  215.         SetCTitle (SkelGetDlogCtl (dlog, button2Item), t2);
  216.         /* fall through... */
  217.     case 1:
  218.         SetCTitle (SkelGetDlogCtl (dlog, button1Item), t1);
  219.     }
  220.  
  221.     ParamText (s1, s2, s3, s4);        /* construct message */
  222.     ShowWindow (dlog);
  223.  
  224.     /*
  225.      * Given a nil filter proc, ModalDialog() normally returns 1 if Return or
  226.      * Enter are hit.  This can be incorrect since the default item need not
  227.      * be one for the "alert" being presented here.  This problem does not
  228.      * actually occur since (i) a non-nil filter is installed by SkelDlogFilter()
  229.      * and (ii) the filter used by SkelDlogFilter() returns the item number in
  230.      * the aDefItem field.
  231.      */
  232.  
  233.     filter = SkelDlogFilter (nil, true);
  234.     if (cancelButton != 0)
  235.         SkelDlogCancelItem (cancelButton);
  236.     ModalDialog (filter, &item);
  237.     SkelRmveDlogFilter ();
  238.     HPurge (iListHandle);
  239.     DisposeDialog (dlog);    /* this also disposes of iListHandle */
  240.     SetPort (savePort);
  241.     return (item);
  242. }
  243.